package UILayer;

import java.util.ArrayList;
import java.util.Scanner;

import CtrLayer.VareCtr;
import ModelLayer.ProduktGruppe;
import ModelLayer.Vare;

public class VareUI {
	private VareCtr vCtr;

	public VareUI() {
		vCtr = new VareCtr();
	}

	/**
	 * Start Vare UI method.
	 * 
	 * */
	public void startVareUI() {
		boolean start = false;
		while (!start) {
			Scanner input = new Scanner(System.in);
			System.out.println("1. Add Vare");
			System.out.println("2. Remove Vare");
			System.out.println("3. Find Vare");
			System.out.println("4. Create produkt gruppe");
			System.out.println("5. Remove produkt gruppe");
			System.out.println("6. Add Vare to Produktgruppe");
			System.out.println("7. List of all varer");

			int choice = input.nextInt();
			switch (choice) {
			case 1:
				createVare();
				break;
			case 2:
				removeVare();
				break;
			case 3:
				findVare();
				break;
			case 4:
				createProduktGruppe();
				break;
			case 5:
				removeProduktGruppe();
				break;
			case 6:
				addVareToProduktGruppe();
				break;
			case 7:
				listAllVarer();
				break;
			default:
				System.out.println("Quitting VareUI!");
				start = true;
				break;
			}
		}
	}

	/**
	 * Add Vare
	 * 
	 * @param int artikelNr, int vaegt, float pris, String vareGruppe, String
	 *        placering, int antal
	 * */
	public void createVare() {
		Scanner input = new Scanner(System.in);
		System.out.println("Enter artikelNr.: ");
		int aN = input.nextInt();

		Vare v = vCtr.findVare(aN);

		if (v != null) {
			System.out.println("A vare already exits with that artikelnr!");
		} else {
			System.out.println("Enter weight: ");
			int vg = input.nextInt();
			System.out.println("Enter price: ");
			float pris = input.nextFloat();
			System.out.println("Enter vareGruppe: ");
			String vareG = input.next();

			ProduktGruppe pg = vCtr.findPG(Integer.parseInt(vareG));
			if (pg != null) {
				System.out.println("Enter placering: ");
				String pla = input.next();
				System.out.println("Enter amount: ");
				int antal = input.nextInt();

				vCtr.createVare(aN, vg, pris, vareG, pla, antal);
				System.out.println("Vare added to system!");
			} else {
				System.out.println("A Produktgruppe with that gruppenr does not exist!");
			}
		}
	}

	/**
	 * Remove vare from system.
	 * 
	 * @param int artikelNr
	 * */
	public void removeVare() {
		Scanner input = new Scanner(System.in);
		System.out.println("Enter artikelNr.: ");
		int aN = input.nextInt();

		Vare v = vCtr.findVare(aN);

		if (v != null) {
			vCtr.removeVare(aN);
			System.out.println("Vare has been removed!!");
		} else {
			System.out.println("No vare was found with that artikelnr!");
		}
	}

	/**
	 * Find vare
	 * 
	 * @param int artikelNr.
	 * */
	public void findVare() {
		Scanner input = new Scanner(System.in);
		System.out.println("Enter artikelNr.: ");
		int aN = input.nextInt();
		Vare v = vCtr.findVare(aN);
		System.out.println("Artikel Nr.: " + v.getArtikelNr());
		System.out.println("Vaegt: " + v.getVaegt());
		System.out.println("Pris: " + v.getPris());
		System.out.println("Vare Gruppe: " + v.getVareGruppe());
		System.out.println("Placering: " + v.getPlacering());
		System.out.println("Antal: " + v.getAntal());
	}

	/**
	 * Create Produkt gruppe.
	 * 
	 * @param String
	 *            beskrivelse, int gruppeNr
	 * */
	public void createProduktGruppe() {
		Scanner input = new Scanner(System.in);
		System.out.println("Enter beskrivelse: ");
		String bs = input.next();
		System.out.println("Enter gruppeNr: ");
		int gN = input.nextInt();

		ProduktGruppe pg = vCtr.findPG(gN);
		if (pg != null) {
			System.out.println("A Produktgruppe with that gruppenr does exist!");
		} else {
			vCtr.createProduktGruppe(bs, gN);
			System.out.println("Produktgruppe has been created!");
		}
	}

	/**
	 * Remove produkt gruppe.
	 * 
	 * @param int gruppeNr
	 * */
	public void removeProduktGruppe() {
		Scanner input = new Scanner(System.in);
		System.out.println("Enter gruppe Nr.: ");
		int gN = input.nextInt();

		ProduktGruppe pg = vCtr.findPG(gN);
		if (pg != null) {
			vCtr.removeProduktGruppe(gN);
			System.out.println("Produktgruppe has been removed!!");
		} else {
			System.out
					.println("A Produktgruppe with that gruppenr does not exist!");
		}
	}

	/**
	 * Add vare to produktgruppe
	 * 
	 * @param int gruppeNr, int artikelNr
	 * */
	public void addVareToProduktGruppe() {
		Scanner input = new Scanner(System.in);
		System.out.println("Enter gruppe nr: ");
		int gN = input.nextInt();

		ProduktGruppe pg = vCtr.findPG(gN);
		if (pg != null) {
			System.out.println("Enter artikelNr: ");
			int aN = input.nextInt();

			Vare v = vCtr.findVare(aN);

			if (v != null) {
				vCtr.addVareToProduktGruppe(gN, aN);
				System.out.println("Vare has been added!");
			} else {
				System.out.println("No vare was found with that artikelnr!");
			}

		} else {
			System.out
					.println("A Produktgruppe with that gruppenr does not exist!");
		}

	}

	/**
	 * List all varer
	 * */
	public void listAllVarer() {
		ArrayList<Vare> vList = vCtr.listOfAllVarer();
		for (Vare x : vList) {
			System.out.println("ArtikelNr: " + x.getArtikelNr());
			System.out.println("Pris: " + x.getPris());
			System.out.println("Placering: " + x.getPlacering());
			System.out.println("Antal: " + x.getAntal());
			System.out.println("--------------------------------------------");
		}
	}
}
